/*******************************************************************************
* CHIPSEA F60 IIC HAL
*******************************************************************************/

#include "hal_cs_f60_iic.h"

#ifdef CS_F60

#include <stdint.h>


#include "user_interface.h"

volatile uint8_t iicErrTime = 0;
//uint8_t g_bDeepSleep = 0;

/**********************************************************************
@brief : IIC 初始化
@param : NONE
***********************************************************************/
void User_IIC_Slave_Init(void)
{
	RCC_APBPeriphClOCKCmd(RCC_APBPeriph_I2C,ENABLE);	//enable the clock

	NVIC_IRQxEnable(IRQ_I2C);				// enable iic interrupt
	NVIC_IRQPriorityConfig(IRQ_I2C,0);			// set ISR priority highest 

	System_SetGPIOReuse(IIC_SDA, I2C_SDA); 		//设置sda功能
	System_SetGPIOReuse(IIC_SCL, I2C_SCL); 		//设置Pscl功能

	I2C_IntAllDisable();				//unmask all interrupt firstly
	I2C_IntModeEnable(I2C_RD_REQ | I2C_RX_FULL | I2C_R_STOP);

	I2C_InitTypeDef I2C_InitStruct;

	I2C_InitStruct.I2C_Addr			= IIC_ADDR; //I2CSLAVE_ADDR;
	I2C_InitStruct.I2C_RX_FIFO_Level	= IIC_RX_FIFO_LEVEL; //1; //once one byte received,interrupt generated
	I2C_InitStruct.I2C_TX_FIFO_Level	= IIC_TX_FIFO_LEVEL; //8;

	I2C_Salve_Init(&I2C_InitStruct);		//initialize it

	//I2C_State_Init();

	I2C_Enable();
}

/***********************************************************************
@brief : 初始化IIC相关的状态，如地址匹配，等待寄存器地址，一次通信完成
@param : NONE
************************************************************************/
/*
static void I2C_State_Init(void)
{
	IIC_State.Is_Wait_RegAddr = YES;
	IIC_State.I2C_Cmd_Done    = NO;
	IIC_State.I2C_SlaveAddr_Matched = NO;
}
*/

/***********************************************************************
@brief : I2C ISR
@param : NONE
************************************************************************/

void I2C0_IRQHandler(void)
{
	uint8_t ret  = 0xFF;
	uint8_t data = 0xFF;
	
	st_power.state = I2C_START;

	if(I2C0->I2C_INT_STAT & BIT(15))    //MAT_SAR 深度睡眠模式下，从机地址匹配 写(0xA0)或读(0xA1)都匹配
	{
		ret = I2C0->I2C_CLR_MAT_SAR & BIT(0); //清除匹配唤醒标志位
		//g_bDeepSleep = 0;
	}
	else if(I2C0->I2C_INT_STAT & I2C_RD_REQ)      //Master read request
	{
		I2C0->I2C_CLR_RD_REQ;
        
		if(st_iic.r_pointer < st_iic.r_count)     
		{
			data = *(st_iic.data_ptr + st_iic.r_pointer) ;
			st_iic.r_pointer++;
		}

		I2C_SendByte(data);
	}
	else if(I2C0->I2C_INT_STAT & I2C_RX_FULL)                 // slaver receive 1 byte
	{        
		ret = I2C0->I2C_DATA_CMD;//I2C_ReadByte();

		if(st_iic.control_flag == false)                 //reg addr
		{
			st_iic.control_flag = true;

			register_select(ret);
			st_iic.w_pointer = 0;
		}
		else
		{
			if(st_iic.w_pointer < st_iic.w_count)       //write data to reg 
			{
				*(st_iic.data_ptr + st_iic.w_pointer) = ret;           
				st_iic.w_pointer++;
			}
		}
	}
	else if(I2C0->I2C_INT_STAT & I2C_R_STOP)                    // STOP
	{
		I2C0->I2C_CLR_STOP_DET;

		st_iic.control_flag = false;
		st_iic.r_pointer = 0;
		st_iic.w_pointer = 0;
	}

	iicErrTime = 0;
}

/*******************************************************************************
@brief : I2C0_BusyError
@param : NONE
*******************************************************************************/
void hal_i2c_busy_error(void)
{
	if(I2C0 -> I2C_STATUS&BIT(6))
	{
		iicErrTime++;
	}

	if(iicErrTime > 2)
	{
		Reg_WRProtDisable(); //

		RSTMU->IPRSTC2 |= BIT(8);

		Reg_WRProtEnable(); //
		Reg_WRProtDisable(); //

		RSTMU->IPRSTC2 &= ~BIT(8);

		Reg_WRProtEnable(); //
        
		User_IIC_Slave_Init();
        
		iicErrTime = 0;
	}
}

#endif /* End of CS_F60 */
